package com.tom.service.impl;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tom.constants.BaseConstants;
import com.tom.constants.UserConstant;
import com.tom.domain.Login;
import com.tom.domain.VipBase;
import com.tom.domain.VipUser;
import com.tom.dto.SMSRegisterDto;
import com.tom.exception.GloblException;
import com.tom.feign.clients.LoginFeignClient;
import com.tom.mapper.VipBaseMapper;
import com.tom.mapper.VipUserMapper;
import com.tom.service.IVipUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.tom.util.AjaxResult;
import com.tom.util.AssertUtil;
import com.tom.util.BitStatesUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author pengh.liu
 * @since 2021-12-02
 */
@Service
// TODO 分布式事务

public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {
    @Autowired
    private VipBaseMapper vipBaseMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginFeignClient loginFeignClient;

    /**
     * 手机号注册
     * @param dto
     */
    @Override
    @GlobalTransactional
    public void SMSRegister(SMSRegisterDto dto) {
        // 判断参数
        String mobile = dto.getMobile();
        String password = dto.getPassword();
        Integer regChannel = dto.getRegChannel();
        String smsCode = dto.getSmsCode();
        AssertUtil.isNotEmpty(mobile, "手机号码不能为空");
        AssertUtil.isNotEmpty(password, "密码为空");
        AssertUtil.isNotNull(regChannel, "注册方式不能为空");
        AssertUtil.isNotEmpty(smsCode, "验证码不能为空");

        // 判断手机号是否被注册过
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq("phone", mobile);
        VipUser vipUser = super.selectOne(wrapper);
        AssertUtil.isNull(vipUser, "该手机号被注册过");

        // 判断验证码是否正确
        Object o = redisTemplate.opsForValue().get(UserConstant.REGISTER_PREFIX + mobile);
        AssertUtil.isNotNull(o, "请重新获取验证码，验证码已经过期");
        if (!o.toString().split(":")[1].equalsIgnoreCase(smsCode)){
            throw new GloblException("验证码错误，请重新输入");
        }

        // 保存login
        Login login = new Login();
        // 前台用户
        login.setType(BaseConstants.Login.LOGIN_FRONTDESK);
        // TODO 密码需要加盐加密
        login.setPassword(password);
        login.setUsername(mobile);
        AjaxResult save = loginFeignClient.save(login);
        if (!save.isSuccess()){
            throw new GloblException("存到login表中失败");
        }
        // 返回自增主键之后的loginId
        Long loginId = Long.valueOf(save.getResultObj().toString());


        // 保存user
        VipUser user = new VipUser();
        user.setLoginId(loginId);
        user.setPhone(mobile);
        user.setBitState(user.getBitState()+BitStatesUtils.OP_REGISTED);
        user.setBitState(user.getBitState()+BitStatesUtils.OP_AUTHED_PHONE);
        Date date = new Date();
        user.setCreateTime(date.getTime());
        super.insert(user);

        // 保存base表
        VipBase vipBase = new VipBase();
        vipBase.setSsoId(user.getId());
        vipBase.setRegChannel(regChannel);
        vipBase.setCreateTime(date.getTime());
        vipBase.setRegTime(date.getTime());
        vipBaseMapper.insert(vipBase);

        // 删除电话验证码
        redisTemplate.delete(UserConstant.REGISTER_PREFIX+mobile);
        int i = 1/0;

    }
}
